using UnityEngine;
using System.Collections;

public class Paddle : MonoBehaviour
{

	private float mfMoveSpeed = 30;  //velocidad del paddle
	private float mfMoveSpeedTouch = 5;
	private float mfSeparacionLateral = 14.5f; //Separacion del paddle con los lados
	
	
		
	void Update()
	{
		
		float moveInput = Input.GetAxis("Horizontal") * Time.deltaTime * mfMoveSpeed;
		transform.position += new Vector3(moveInput, 0, 0);
		//Debug.Log(moveInput);

		if (transform.position.x <= -mfSeparacionLateral || transform.position.x >= mfSeparacionLateral)
		{
			float xPos = Mathf.Clamp(transform.position.x, -mfSeparacionLateral, mfSeparacionLateral); //Clamp between min -5 and max 5
			transform.position = new Vector3(xPos, transform.position.y, transform.position.z);
		}

		
		foreach (Touch touch in Input.touches)
		{
			if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
			{
				//print("User has finger(s) touching the screen");
				//SpawnBall(true);
				
				BreakoutGame.SP.mbTouch = true;
			}
		}
		

		if (Input.touches.Length > 0)
        {
        // Only work with the first touch
        // and only if the touch moved since last update
	        if (Input.touches[0].phase == TouchPhase.Moved)
	        {
	            float lfX = Input.touches[0].deltaPosition.x * mfMoveSpeedTouch * Time.deltaTime;
	            //float y = Input.touches[0].deltaPosition.y * mfMoveSpeedTouch * Time.deltaTime;
				
				transform.Translate(new Vector3(lfX, 0, 0));
	        }
        }
		
		
	}

	void OnCollisionExit(Collision collisionInfo)
	{
		//Add X velocity..otherwise the ball would only go up&down
		Rigidbody rigid = collisionInfo.rigidbody;
		float xDistance = rigid.position.x - transform.position.x;
		rigid.velocity = new Vector3(rigid.velocity.x + xDistance / 2, rigid.velocity.y, rigid.velocity.z);
	}
	
	
}
